/* 自定义路由中间件 */

//引入express框架
const express = require("express");
//引入boom依赖快速生成浏览器错误信息
const boom = require("boom");
// 引入utils包
const { verifyToken } = require("../config/utils");
//导入二级路由
const userRouter = require("../page/user");   // 用户信息接口
const mes_dv_machinery_router = require("../page/mes/dv/machinery");   // 设备管理模块
const mes_dv_machinerytype_router = require("../page/mes/dv/machinerytype");   // 设备类型管理模块

const mes_pro_workorder_router = require("../page/mes/pro/workorder");   // 生产管理模块
const mes_pro_process_router = require("../page/mes/pro/process");   // 工序管理模块
const mes_pro_processcontent_router = require("../page/mes/pro/processcontent");   // 工序内容管理模块

const mes_md_itemtype_router = require("../page/mes/md/itemtype");   // 物料分类模块
const mes_md_item_router = require("../page/mes/md/mdItem");   // 物料信息管理模块
const mes_md_unitmeasure_router = require("../page/mes/md/unitmeasure");   // 计量单位模块
const mes_md_bom_router = require("../page/mes/md/bom");   // BOM管理模块
const mes_md_sop_router = require("../page/mes/md/sop");   // SOP管理模块
const mes_md_client_router = require("../page/mes/md/client");   // 客户管理模块
const mes_md_vendor_router = require("../page/mes/md/vendor");   // 供应商管理模块
const mes_md_workshop_router = require("../page/mes/md/workshop");   // 车间管理模块
const mes_md_workstation_router = require("../page/mes/md/workstation");   // 工位管理模块
const mes_md_workstationmachine_router = require("../page/mes/md/workstationmachine");   // 工位设备管理模块
const mes_md_workstationtool_router = require("../page/mes/md/workstationtool");   // 工位工装管理模块
const mes_md_workstationworker_router = require("../page/mes/md/workstationworker");   // 工位人员管理模块

const mes_wm_warehouse_router = require("../page/mes/wm/warehouse");   // 仓库管理模块

const mes_tm_tooltype_router = require("../page/mes/tm/tooltype");   // 工装夹具类型管理模块
const mes_tm_tool_router = require("../page/mes/tm/tool");   // 工装夹具管理模块

const sys_post_router = require("../page/system/post");   // 岗位管理模块
const sys_user_router = require("../page/system/user");   // 用户管理模块
const sys_dept_router = require("../page/system/dept");   // 部门管理模块
const sys_config_router = require("../page/system/config");   // 参数设置模块
const sys_role_router = require("../page/system/role");   // 角色管理模块
const sys_menu_router = require("../page/system/menu");   // 菜单管理模块
const sys_notice_router = require("../page/system/notice");   // 通知公告模块

const system_dict_data_router = require("../page/system/dict/data");   // 字典数据模块
const system_dict_type_router = require("../page/system/dict/type");   // 字典类型模块

const system_autocode_router = require("../page/system/autocode/rule");   // 自动生成代码模块

const common_router = require("../page/common");   // 公共接口

// const {logRouter} = require("../page/log");   // 日志接口


// 通过express框架创建路由实例来处理路由监听
const router = express.Router();

//监听get请求
router.get("/", (req, res) => {
    res.send("首页");
});

//定义token验证中间件
router.use((req, res, next) => {
  // // 测试接口
  // next()
  // return

  // 过滤token验证的路由
  let reg2 = /\/img_uploads\/\d{4}\/\d{2}\/\d{2}\/(.*)$/;// 匹配图片路径
  const urlArr = ["/login", "/captchaImage", "/logout"];
  if(urlArr.includes(req.url) || req.url.match(reg2)){
    next()
    return
  }

  // 获取token
  const token = req.headers.authorization;
  //判断token是否存在
  if(token){
    // 验证token时间是否过期
    if(verifyToken(token)){
      next()
    }else{
      res.send({code:401,errCode:"-1",msg:"token过期"})
    }
  }else{
    res.send({code:401,errCode:"-1",msg:"token不存在"})
  }
});

//监听到一级路由是/user是使用自定义二级路由userRouter中间件
router.use("/", userRouter);

router.use("/mes/dv/machinery", mes_dv_machinery_router);
router.use("/mes/dv/machinerytype", mes_dv_machinerytype_router);

router.use("/mes/pro/workorder", mes_pro_workorder_router);
router.use("/mes/pro/process", mes_pro_process_router);
router.use("/mes/pro/processcontent", mes_pro_processcontent_router);

router.use("/mes/md/itemtype", mes_md_itemtype_router);
router.use("/mes/md/mditem", mes_md_item_router);
router.use("/mes/md/unitmeasure", mes_md_unitmeasure_router);
router.use("/mes/md/bom", mes_md_bom_router);
router.use("/mes/md/sop", mes_md_sop_router);
router.use("/mes/md/client", mes_md_client_router);
router.use("/mes/md/vendor", mes_md_vendor_router);
router.use("/mes/md/workshop", mes_md_workshop_router);
router.use("/mes/md/workstation", mes_md_workstation_router);
router.use("/mes/md/workstationmachine", mes_md_workstationmachine_router);
router.use("/mes/md/workstationtool", mes_md_workstationtool_router);
router.use("/mes/md/workstationworker", mes_md_workstationworker_router);

router.use("/mes/wm/warehouse", mes_wm_warehouse_router);

router.use("/mes/tm/tooltype", mes_tm_tooltype_router);
router.use("/mes/tm/tool", mes_tm_tool_router);

router.use("/system/post", sys_post_router);
router.use("/system/user", sys_user_router);
router.use("/system/dept", sys_dept_router);
router.use("/system/config", sys_config_router);
router.use("/system/role", sys_role_router);
router.use("/system/menu", sys_menu_router);
router.use("/system/notice", sys_notice_router);

router.use("/system/dict/data", system_dict_data_router);
router.use("/system/dict/type", system_dict_type_router);

router.use("/system/autocode", system_autocode_router);

router.use("/common", common_router);
// router.use("/log", logRouter);


//路由匹配是从上到下的,所以当前面所有的路由地址都不匹配时加载请求404错误中间件
router.use((req, res, next) => {
  next(boom.notFound("接口不存在"));
});

//定义一个错误处理中间件 向客户端响应json格式的错误数据
router.use((err, req, res, next) => {
  //获取错误信息
  const msg = (err && err.message) || "系统错误";
  //获取错误状态码
  const statusCode = (err.output && err.output.statusCode) || 500;
  //获取errorMsg
  const errorMsg = (err.output && err.output.payload && err.output.payload.error) || err.message;
  //返回状态码，并返回json格式错误异常信息
  res.status(statusCode).json({
    code: -1,
    msg,
    error: statusCode,
    errorMsg,
  });
});

module.exports = router;